Load all required libraries.

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.6.3
## -- Attaching packages ----------------------------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.3     v dplyr   1.0.0
## v tidyr   1.1.0     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## Warning: package 'ggplot2' was built under R version 3.6.3
## Warning: package 'tibble' was built under R version 3.6.3
## Warning: package 'readr' was built under R version 3.6.3
## Warning: package 'dplyr' was built under R version 3.6.3
## Warning: package 'forcats' was built under R version 3.6.3
## -- Conflicts -------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## Warning: package 'plotly' was built under R version 3.6.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)
## Warning: package 'broom' was built under R version 3.6.3

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
        p2
## Warning: `group_by_()` is deprecated as of dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 310)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 12.95018 12.95048 12.95079 12.95112 12.95146 12.95180 12.95215 12.95249
##   [9] 12.95283 12.95315 12.95345 12.95372 12.95397 12.95419 12.95436 12.95449
##  [17] 12.95458 12.95461 12.95458 12.95449 12.95433 12.95409 12.95378 12.95339
##  [25] 12.95290 12.95233 12.95165 12.95087 12.94999 12.94899 12.94789 12.94669
##  [33] 12.94540 12.94402 12.94256 12.94104 12.93945 12.93780 12.93610 12.93436
##  [41] 12.93259 12.93079 12.92896 12.92712 12.92527 12.92342 12.92158 12.91975
##  [49] 12.91794 12.91615 12.91441 12.91270 12.91104 12.90943 12.90789 12.90641
##  [57] 12.90502 12.90351 12.90172 12.89965 12.89731 12.89472 12.89189 12.88883
##  [65] 12.88554 12.88205 12.87836 12.87449 12.87044 12.86623 12.86187 12.85737
##  [73] 12.85274 12.84799 12.84314 12.83819 12.83316 12.82806 12.82289 12.81768
##  [81] 12.81243 12.80716 12.80187 12.79658 12.79129 12.78603 12.78079 12.77560
##  [89] 12.77046 12.76539 12.76040 12.75549 12.75068 12.74598 12.74140 12.73696
##  [97] 12.73266 12.72852 12.72454 12.72075 12.71714 12.71374 12.71055 12.70758
## [105] 12.70485 12.70237 12.70015 12.69819 12.69652 12.69514 12.69407 12.69331
## [113] 12.69288 12.69275 12.69290 12.69332 12.69401 12.69496 12.69619 12.69767
## [121] 12.69942 12.70144 12.70370 12.70623 12.70901 12.71204 12.71532 12.71885
## [129] 12.72263 12.72665 12.73092 12.73542 12.74016 12.74514 12.75036 12.75581
## [137] 12.76148 12.76739 12.77353 12.77988 12.78799 12.79905 12.81261 12.82820
## [145] 12.84539 12.86369 12.88265 12.90182 12.92074 12.93894 12.95597 12.97137
## [153] 12.98468 12.99544 13.00549 13.01691 13.02959 13.04345 13.05837 13.07427
## [161] 13.09103 13.10857 13.12678 13.14556 13.16482 13.18445 13.20436 13.22444
## [169] 13.24460 13.26474 13.28476 13.30455 13.32403 13.34308 13.36162 13.37954
## [177] 13.39674 13.41313 13.42860 13.44305 13.45639 13.46851 13.47933 13.48873
## [185] 13.49662 13.50290 13.50746 13.51022 13.51107 13.51043 13.50880 13.50621
## [193] 13.50268 13.49825 13.49294 13.48678 13.47979 13.47201 13.46347 13.45419
## [201] 13.44419 13.43351 13.42218 13.41022 13.39766 13.38453 13.37085 13.35666
## [209] 13.34198 13.32684 13.30940 13.28816 13.26366 13.23644 13.20707 13.17607
## [217] 13.14400 13.11140 13.07882 13.04680 13.01590 12.98664 12.95959 12.93528
## [225] 12.91148 12.88569 12.85809 12.82884 12.79813 12.76612 12.73298 12.69890
## [233] 12.66403 12.62856 12.59265 12.55648 12.52022 12.48404 12.44811 12.41262
## [241] 12.37772 12.34359 12.31041 12.27835 12.24757 12.21825 12.19057 12.16469
## [249] 12.14080 12.11905 12.09962 12.08269 12.06736 12.05261 12.03843 12.02483
## [257] 12.01180 11.99934 11.98744 11.97610 11.96533 11.95510 11.94544 11.93632
## [265] 11.92774 11.91972 11.91223 11.90528 11.89880 11.89275 11.88716 11.88204
## [273] 11.87743 11.87333 11.86978 11.86681 11.86443 11.86266 11.86154 11.86108
## [281] 11.86131 11.86226 11.86386 11.86607 11.86887 11.87227 11.87626 11.88085
## [289] 11.88604 11.89182 11.89820 11.90517 11.91274 11.92090 11.92966 11.93902
## [297] 11.94897 11.95951 11.97065 11.98238 11.99470 12.00762 12.02113 12.03524
## [305] 12.04993 12.06522 12.08111 12.09758 12.11465 12.13231
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.6, n = 310)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.59653 12.59338 12.59033 12.58738 12.58451 12.58173 12.57903 12.57641
##   [9] 12.57387 12.57140 12.56900 12.56666 12.56438 12.56215 12.55999 12.55787
##  [17] 12.55579 12.55376 12.55177 12.54981 12.54788 12.54598 12.54411 12.54225
##  [25] 12.54041 12.53859 12.53677 12.53496 12.53315 12.53133 12.52949 12.52765
##  [33] 12.52580 12.52395 12.52211 12.52028 12.51848 12.51670 12.51495 12.51324
##  [41] 12.51157 12.50996 12.50839 12.50689 12.50546 12.50410 12.50282 12.50162
##  [49] 12.50052 12.49951 12.49861 12.49781 12.49713 12.49656 12.49613 12.49583
##  [57] 12.49566 12.49557 12.49548 12.49541 12.49535 12.49531 12.49530 12.49532
##  [65] 12.49538 12.49548 12.49563 12.49583 12.49609 12.49641 12.49681 12.49727
##  [73] 12.49781 12.49844 12.49915 12.49997 12.50088 12.50189 12.50301 12.50425
##  [81] 12.50561 12.50710 12.50872 12.51047 12.51236 12.51440 12.51660 12.51895
##  [89] 12.52146 12.52413 12.52699 12.53001 12.53298 12.53567 12.53810 12.54030
##  [97] 12.54230 12.54412 12.54579 12.54735 12.54881 12.55020 12.55155 12.55288
## [105] 12.55423 12.55562 12.55708 12.55862 12.56029 12.56211 12.56410 12.56629
## [113] 12.56871 12.57139 12.57434 12.57761 12.58121 12.58517 12.58953 12.59429
## [121] 12.59945 12.60492 12.61070 12.61676 12.62308 12.62964 12.63641 12.64338
## [129] 12.65053 12.65784 12.66528 12.67283 12.68048 12.68820 12.69597 12.70378
## [137] 12.71159 12.71940 12.72717 12.73489 12.74446 12.75746 12.77334 12.79158
## [145] 12.81164 12.83301 12.85513 12.87749 12.89955 12.92078 12.94064 12.95862
## [153] 12.97417 12.98677 12.99845 13.01153 13.02589 13.04143 13.05803 13.07560
## [161] 13.09402 13.11318 13.13297 13.15328 13.17400 13.19503 13.21625 13.23756
## [169] 13.25885 13.28000 13.30091 13.32146 13.34156 13.36109 13.37993 13.39799
## [177] 13.41515 13.43131 13.44635 13.46016 13.47264 13.48367 13.49315 13.50097
## [185] 13.50702 13.51119 13.51337 13.51344 13.51131 13.50685 13.50010 13.49123
## [193] 13.48041 13.46781 13.45359 13.43792 13.42096 13.40288 13.38385 13.36402
## [201] 13.34358 13.32268 13.30149 13.28017 13.25889 13.23782 13.21713 13.19697
## [209] 13.17752 13.15894 13.13824 13.11283 13.08343 13.05081 13.01570 12.97885
## [217] 12.94100 12.90291 12.86530 12.82894 12.79457 12.76293 12.73476 12.71081
## [225] 12.68862 12.66529 12.64096 12.61574 12.58973 12.56307 12.53587 12.50824
## [233] 12.48031 12.45218 12.42399 12.39584 12.36785 12.34015 12.31284 12.28605
## [241] 12.25989 12.23448 12.20994 12.18638 12.16393 12.14270 12.12280 12.10436
## [249] 12.08749 12.07231 12.05894 12.04749 12.03741 12.02805 12.01938 12.01139
## [257] 12.00404 11.99733 11.99123 11.98571 11.98077 11.97636 11.97249 11.96911
## [265] 11.96622 11.96378 11.96179 11.96021 11.95907 11.95842 11.95827 11.95864
## [273] 11.95954 11.96099 11.96300 11.96559 11.96877 11.97255 11.97695 11.98198
## [281] 11.98767 11.99402 12.00099 12.00855 12.01668 12.02540 12.03469 12.04456
## [289] 12.05500 12.06603 12.07763 12.08980 12.10255 12.11588 12.12978 12.14425
## [297] 12.15930 12.17491 12.19111 12.20787 12.22520 12.24311 12.26158 12.28062
## [305] 12.30023 12.32042 12.34116 12.36248 12.38436 12.40681
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.6, n = 310)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 11.90468 11.90546 11.90627 11.90713 11.90802 11.90893 11.90985 11.91079
##   [9] 11.91172 11.91265 11.91357 11.91447 11.91534 11.91618 11.91697 11.91771
##  [17] 11.91840 11.91902 11.91957 11.92004 11.92042 11.92071 11.92090 11.92098
##  [25] 11.92094 11.92079 11.92050 11.92007 11.91950 11.91877 11.91789 11.91684
##  [33] 11.91561 11.91420 11.91261 11.91081 11.90880 11.90656 11.90411 11.90146
##  [41] 11.89864 11.89566 11.89254 11.88929 11.88594 11.88250 11.87898 11.87541
##  [49] 11.87181 11.86818 11.86455 11.86094 11.85735 11.85382 11.85035 11.84696
##  [57] 11.84368 11.84051 11.83748 11.83460 11.83189 11.82937 11.82705 11.82496
##  [65] 11.82271 11.81994 11.81667 11.81292 11.80873 11.80412 11.79911 11.79372
##  [73] 11.78800 11.78195 11.77560 11.76898 11.76212 11.75504 11.74776 11.74031
##  [81] 11.73272 11.72501 11.71720 11.70933 11.70141 11.69348 11.68555 11.67765
##  [89] 11.66981 11.66206 11.65441 11.64690 11.63954 11.63237 11.62541 11.61868
##  [97] 11.61221 11.60603 11.60016 11.59462 11.58944 11.58465 11.58028 11.57633
## [105] 11.57285 11.56986 11.56738 11.56544 11.56406 11.56326 11.56309 11.56355
## [113] 11.56467 11.56648 11.56901 11.57228 11.57631 11.58113 11.58677 11.59325
## [121] 11.60153 11.61237 11.62556 11.64085 11.65800 11.67678 11.69695 11.71828
## [129] 11.74053 11.76346 11.78683 11.81042 11.83398 11.85728 11.88007 11.90214
## [137] 11.92322 11.94311 11.96154 11.97830 11.99658 12.01929 12.04574 12.07520
## [145] 12.10699 12.14040 12.17473 12.20926 12.24331 12.27617 12.30713 12.33549
## [153] 12.36056 12.38162 12.40101 12.42148 12.44293 12.46526 12.48839 12.51222
## [161] 12.53664 12.56158 12.58692 12.61259 12.63847 12.66449 12.69053 12.71652
## [169] 12.74235 12.76792 12.79316 12.81795 12.84220 12.86583 12.88873 12.91080
## [177] 12.93197 12.95212 12.97117 12.98902 13.00558 13.02075 13.03443 13.04654
## [185] 13.05697 13.06564 13.07244 13.07728 13.08007 13.08131 13.08155 13.08079
## [193] 13.07906 13.07635 13.07268 13.06804 13.06245 13.05592 13.04844 13.04003
## [201] 13.03069 13.02043 13.00926 12.99718 12.98421 12.97034 12.95559 12.93996
## [209] 12.92345 12.90609 12.88487 12.85746 12.82482 12.78790 12.74767 12.70508
## [217] 12.66111 12.61670 12.57281 12.53042 12.49048 12.45394 12.42178 12.39495
## [225] 12.37043 12.34465 12.31775 12.28984 12.26104 12.23149 12.20131 12.17063
## [233] 12.13956 12.10824 12.07679 12.04533 12.01400 11.98291 11.95219 11.92196
## [241] 11.89236 11.86350 11.83551 11.80852 11.78265 11.75803 11.73478 11.71302
## [249] 11.69289 11.67451 11.65800 11.64348 11.63038 11.61801 11.60633 11.59530
## [257] 11.58490 11.57508 11.56581 11.55705 11.54878 11.54095 11.53353 11.52648
## [265] 11.51978 11.51337 11.50724 11.50134 11.49569 11.49036 11.48537 11.48075
## [273] 11.47654 11.47278 11.46948 11.46668 11.46442 11.46273 11.46163 11.46116
## [281] 11.46135 11.46224 11.46374 11.46577 11.46831 11.47137 11.47494 11.47904
## [289] 11.48364 11.48876 11.49439 11.50054 11.50719 11.51436 11.52203 11.53021
## [297] 11.53889 11.54809 11.55778 11.56798 11.57868 11.58989 11.60159 11.61379
## [305] 11.62649 11.63969 11.65338 11.66757 11.68226 11.69743
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")